import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picList: [],
    saveList: [],
    showDelIcon:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDescription()
  },
  // 点图片显示删除按钮
  clickPic (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      showDelIcon:index
    })
  },
  upPic() {
    var that = this
    wx.chooseImage({
      count: 9, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        for (var j in tempFilePaths) {
          wx.uploadFile({
            url: 'https://lequer.odeman.com/weixin/Decaler/upload_pic?productid=' + wx.getStorageSync('product_id'),
            filePath: tempFilePaths[j],
            name: 'file',
            formData: {
              'user': 'test'
            },
            success: function (res) {
              var data = JSON.parse(res.data)
              if (data.code == 0) {
                var pic = that.data.picList
                pic.push({
                  imgUrl: data.url,
                  content: ''
                })
                that.setData({
                  picList: pic
                })
              }
            }
          })
        }

      }
    })
  },
  // 获取用户输入的内容
  getInputCon(e) {
    var dataset = e.currentTarget.dataset
    var index = dataset.index
    var value = e.detail.value
    var picList = this.data.picList
    picList[index].content = value
    this.setData({
      picList: picList
    })

  },
  // 获取用户上传的活动详情图片
  getDescription () {
    var data = {
      productid:wx.getStorageSync('product_id')
    }
    API.getDescription(data).then(res=>{
      if (res.code==0 && res.description!="") {
        this.setData({
          picList: JSON.parse(res.description)
        })
      }
    })
  },
  save () {
    var list = this.data.picList
    if (list.length>0) {
    var data = {
      description:JSON.stringify(list),
      productid:wx.getStorageSync('product_id')
    }
    API.saveDescription(data).then(res=>{
      if(res.code==0) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);
      }
    })
    wx.setStorageSync('activityDetailPic',true)     
  } else {
    wx.showModal({
      title: '',
      content: '请添加图片文字',
    })
  }
  },
  // 删除图片功能
  delPic (e) {
    var index = e.currentTarget.dataset.index
    var picList = this.data.picList
    picList.splice(index,1)
    this.setData({
      picList:picList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }


})